package com.zzyl.controller.manager;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.dto.AlertRuleQueryDto;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/alert-rule")
@Api(tags = "报警规则相关接口")
public class AlertRuleController {
    @Autowired
    private AlertRuleService alertRuleService;

    /**
     * 新增报警规则
     *
     * @param alertRuleDto 报警规则dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation(value = "新增报警规则")
    @PostMapping("/create")
    public ResponseResult<Void> create(@RequestBody AlertRuleDto alertRuleDto) {
        log.info("新增报警规则:{}", alertRuleDto);
        alertRuleService.createRule(alertRuleDto);
        return ResponseResult.success();
    }

    /**
     * 获取单个告警规则
     *
     * @param id 警告规则id
     * @return {@code ResponseResult<AlertRuleVo> }
     * @author YiSheep
     */
    @ApiOperation(value = "获取单个告警规则")
    @GetMapping("/read/{id}")
    public ResponseResult<AlertRuleVo> read(@PathVariable Long id) {
        log.info("获取单个告警规则:{}", id);
        AlertRuleVo alertRuleVo = alertRuleService.readAlertRule(id);
        return ResponseResult.success(alertRuleVo);
    }

    /**
     * 更新告警规则
     *
     * @param id           警报规则id
     * @param alertRuleDto 警报规则dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation(value = "更新告警规则")
    @PutMapping("/update/{id}")
    public ResponseResult<Void> update(@PathVariable Long id, @RequestBody AlertRuleDto alertRuleDto) {
        log.info("更新告警规则:id {},alertRuleDto {}", id, alertRuleDto);
        alertRuleService.updateAlertRule(id, alertRuleDto);
        return ResponseResult.success();
    }

    /**
     * 删除告警规则
     *
     * @param id 警报规则id
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation(value = "删除告警规则")
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Void> delete(@PathVariable Long id) {
        log.info("删除告警规则:{}", id);
        alertRuleService.deleteAlertRule(id);
        return ResponseResult.success();
    }

    /**
     * 分页获取告警规则列表
     *
     * @param dto 警报规则查询dto
     * @return {@code ResponseResult<PageBean<AlertRuleVo>> }
     * @author YiSheep
     */
    @ApiOperation(value = "分页获取告警规则列表")
    @GetMapping("/get-page")
    public ResponseResult<PageBean<AlertRuleVo>> getPage(AlertRuleQueryDto dto) {
        log.info("分页获取告警规则列表:{}", dto);
        PageBean<AlertRuleVo> pageBean = alertRuleService.getAlertRulePage(dto);
        return ResponseResult.success(pageBean);
    }
    @ApiOperation("启用/禁用")
    @PutMapping("/{id}/status/{status}")
    public ResponseResult<Void> enableOrDisable(@PathVariable Long id, @PathVariable Integer status) {
        log.info("启用/禁用:id {},status {}", id, status);
        alertRuleService.enableOrDisable(id, status);
        return ResponseResult.success();
    }
}
